package com.hotel.servlet;

import com.hotel.bean.Account;
import com.hotel.bean.HotelIdWithRoomType;
import com.hotel.bean.Order;
import com.hotel.bean.OrderWithHotelName;
import com.hotel.dao.AccountDao;
import com.hotel.dao.OrderDao;
import com.hotel.util.DBOperation;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

/**
 * @author KungSu.
 * @create 2021-05-11 14:23
 */
@WebServlet(value = "/InfoServlet")
public class InfoServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        System.out.println(type);
        SqlSession sqlSession = DBOperation.getSqlSession();
        OrderDao orderDao = sqlSession.getMapper(OrderDao.class);
        Account account = (Account) request.getSession().getAttribute("curUser");
        System.out.println("account: " + account);
//        request.setAttribute("account",account);
        switch(type){
//            case "info":
//                request.getRequestDispatcher("/jsp/wodexinxi.jsp").forward(request,response);
//                break;
//            case "change":
//                request.getRequestDispatcher("/jsp/xiugaixinxi.jsp").forward(request,response);
//                break;
            case "jiabinhui":
                List<OrderWithHotelName> orderList = orderDao.findOrderAndHotelName(account.getAccount_id());
                List<OrderWithHotelName> doneOrder = new ArrayList<>();
                List<OrderWithHotelName> undoneOrder = new ArrayList<>();
                //空指针异常
                for (OrderWithHotelName order: orderList){
                    System.out.println(order);
//                    如果订单结束时间小于等于当前时间，则订单结束，将订单信息存入doneOrder列表，反之，存入undoneOrder列表。
                    if (order.getDeparture_time().getTime() <= (new Date().getTime())){
                        doneOrder.add(order);
                    }else{
                        undoneOrder.add(order);
                    }
                }
                sqlSession.close();
                for(Order o: doneOrder){
                    System.out.println("doneOrder:"+o);
                }
                request.setAttribute("doneOrder",doneOrder);
                request.setAttribute("undoneOrder",undoneOrder);
                request.getRequestDispatcher("/jsp/wodejiabinhui.jsp").forward(request,response);
                break;
            case "tuisu":
                String order_id = request.getParameter("order_id");
                orderDao.checkOutOrder(order_id);
                Order order = orderDao.getOrder(order_id);
                System.out.println("order: " + order);
                orderDao.updateRoomState(order);
                String room_type = orderDao.getRoomType(order);
                System.out.println("roomType : " + room_type);
                HotelIdWithRoomType hotelIdWithRoomType = new HotelIdWithRoomType(order.getHotel_id().toString(),room_type);
                orderDao.updateNumber(hotelIdWithRoomType);
                sqlSession.close();
                Enumeration name = request.getParameterNames();
                while (name.hasMoreElements()){
                    System.out.println("request值："+name.nextElement());
                }
                request.getRequestDispatcher("InfoServlet?type=jiabinhui").forward(request,response);
                break;
        }

    }
}
